//
//  TYPlistDefaults.m
//  Tunnel
//
//  Created by wbb on 2022/11/1.
//

#import "TYPlistDefaults.h"

@implementation TYPlistDefaults

//- (BOOL)createPlistDefaults {
//
//    NSFileManager *manager = [NSFileManager defaultManager];
//
//    BOOL isSuccess = NO;
//
//    // 获取文件路径 Documents
//    NSString *plistPath = [TYPlistDefaults path];
//
//    NSAssert(plistPath > 0, @"%s: The length of path should be greater than 0.", __FUNCTION__);
//
//    if (![manager fileExistsAtPath:plistPath]) {
//        // 创建文件目录
//        NSError *error = nil;
//        isSuccess = [manager createFileAtPath:plistPath contents:nil attributes:nil];
//        if (error) {
//            NSLog(@"Create directory failed: %@",[error localizedDescription]);
//        }
//    }
//    return isSuccess;
//}
+ (BOOL)createFile {
    NSString *filePath = [TYPlistDefaults path];

    NSAssert(filePath > 0, @"%s: The length of filePath should be greater than 0.", __FUNCTION__);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 提取出文件存放的目录
    NSString *dir = [filePath stringByDeletingLastPathComponent];
    // 判断目录是否已经存在
    if (![fileManager fileExistsAtPath:dir]) {
        // 如果还没有存在，则需要新建
        NSError *error = nil;
        [fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"Create directory failed: %@",[error localizedDescription]);
            return NO;
        }
    }
    // 创建文件
    return [fileManager createFileAtPath:filePath contents:nil attributes:nil];
}


+ (BOOL)saveFileWithDic:(id)data {
    NSString *filePath = [TYPlistDefaults path];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 提取出文件存放的目录
    NSString *dir = [filePath stringByDeletingLastPathComponent];
    
    BOOL isSuccess = NO;
    // 判断目录是否已经存在
    if ([fileManager fileExistsAtPath:dir])  {
        isSuccess = [data writeToFile:filePath atomically:YES];
    }
    return isSuccess;
}

+ (NSDictionary *)getData {
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[TYPlistDefaults path]];
    return dic?:@{};
}
+ (NSString *)path {
    return  [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/ty.plist"]];
}
@end
